package ControllerLayer;

import ModelLayer.*;
import DBLayer.*;
import java.util.ArrayList;

/**
 *
 * @author Gruppe 2
 */
public class PersonController {

    private static PersonController instance;
    private DbCustomer dbCustomer = new DbCustomer();
    private DbSupplier dbSupplier = new DbSupplier();

    private PersonController()
    {
    }

    public static synchronized PersonController getInstance()
    {
        if (instance == null)
        {
            instance = new PersonController();
        }
        return instance;
    }

    public boolean createCustomer(int personID, String name, String address, int zipCode, String city, String phoneNo, String type, String country, int zipCityID, int countryID)
    {

        Customer customer = new Customer();
        customer.setPersonID(personID);
        customer.setName(name);
        customer.setAddress(address);
        customer.setZipCode(zipCode);
        customer.setCity(city);
        customer.setPhoneNo(phoneNo);
        customer.setCountry(country);
        customer.setZipCityID(zipCityID);
        customer.setCountryID(countryID);
        customer.setType(type);
        boolean affected = false;
        if (dbCustomer.insertCustomer(customer) > 0)
        {
            affected = true;
        }


        return affected;

    }

    //   public boolean createSupplier(int personID, String name, String address, int zipCode, String city, String phoneNo, String type, String country, int zipCityID, String email)
    // {
    // }
//    public ArrayList<Person> listAllPersons()
//    {
//
//    }
//
//    public ArrayList<Customer> listAllCustomers()
//    {
//
//    }
//
//    public ArrayList<Supplier> listAllSuppliers()
//    {
//
//    }
    public boolean deleteCustomer(int id)
    {
        if (dbCustomer.deleteCustomer(id) > 0)
        {
            return true;
        }
        return false;
    }

    public ArrayList<Customer> searchCustomerById(int id)
    {
        ArrayList<Customer> customer = new ArrayList<Customer>();
        customer.add(dbCustomer.searchCustomerId(id));
        return customer;
    }

    public ArrayList<Customer> searchCustomerByName(String name)
    {
        return dbCustomer.searchCustomersByName(name);
    }

    public boolean updateCustomer(int personID, String name, String address, int zipCode, String city, String phoneNo, String type, String country, int zipCityID, int countryID)
    {

        Customer cus = new Customer(personID, name, address, zipCode, city, phoneNo, type, country, zipCityID, countryID);
        if (dbCustomer.updateCustomer(cus) == -1)
        {
            return false;
        }
        else
        {
            return true;
        }
    }

    public boolean updateSupplier(Supplier sup)
    {
        
        if (dbSupplier.updateSupplier(sup) == -1)
        {
            return false;
        }
        else
        {
            return true;
        }
    }


    public boolean createSupplier(int personID, String name, String address, int zipCode, String city, String phoneNo, String type, String country, int zipCityID, int countryID, String email)
    {

        Supplier supplier = new Supplier();
        supplier.setPersonID(personID);
        supplier.setName(name);
        supplier.setAddress(address);
        supplier.setZipCode(zipCode);
        supplier.setCity(city);
        supplier.setPhoneNo(phoneNo);
        supplier.setCountry(country);
        supplier.setZipCityID(zipCityID);
        supplier.setCountryID(countryID);
        supplier.setType(type);
        supplier.setEmail(email);
        boolean affected = false;
        if (dbSupplier.insertSupplier(supplier) > 0)
        {
            affected = true;
        }


        return affected;

    }

    public ArrayList<Customer> getAllCustomers()
    {
        return dbCustomer.getAllCustomer();
    }

      public ArrayList<Supplier> getAllSuppliers()
    {
        return dbSupplier.getAllSupplier();
    }

    //   public boolean createSupplier(int personID, String name, String address, int zipCode, String city, String phoneNo, String type, String country, int zipCityID, String email)
    // {
    // }
//    public ArrayList<Person> listAllPersons()
//    {
//
//    }
//
//    public ArrayList<Customer> listAllCustomers()
//    {
//
//    }
//
//    public ArrayList<Supplier> listAllSuppliers()
//    {
//
//    }
    public boolean deleteSupplier(int id)
    {
        if (dbSupplier.deleteSupplier(id) > 0)
        {
            return true;
        }
        return false;
    }

    public ArrayList<Supplier> searchSupplierById(String id)
    {
        return dbSupplier.searchSuppliersById(id);
    }

    public ArrayList<Supplier> searchSupplierByName(String name)
    {
        return dbSupplier.searchSuppliersByName(name);
    }

    public int getNextId()
    {
        int MaxID = GetMax.getMaxId("SELECT MAX(personID) FROM Person");
        return MaxID + 1;
    }

    public String getSupCity(int zip)
    {
        return dbSupplier.getCity(zip);
    }

    public int getSupCityId(int zip)
    {
        return dbSupplier.getCityID(zip);
    }

    public String getCusCity(int zip)
    {
        return dbCustomer.getCity(zip);
    }

    public String getContryName(String contryCode)
    {
        return dbCustomer.getCountry(contryCode);
    }


    public int getContryId(String cCode)
    {
        return dbCustomer.getContryId(cCode);
    }
}
